/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI.Tree;

import Common.IDocument;
import UI.AddDialog;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author Администратор
 */
public class DocumentNode extends DefaultMutableTreeNode implements ITree, IContextCommand {

    private IDocument document;
    private ProjectNode projectNode;
    private JPopupMenu popupMenu;

    public DocumentNode(IDocument document, ProjectNode projectNode) {
        super(document.getName(), true);
        this.document = document;
        this.projectNode = projectNode;
        this.setUserObject(this.document.getName());
        //-------------------------------------------
        this.popupMenu = new JPopupMenu();
        this.popupMenu.add(new ContextCommandItem(this, "Rename Document"));
        this.popupMenu.add(new ContextCommandItem(this, "Delete Document"));
        //-------------------------------------------
    }

    // <editor-fold defaultstate="collapsed" desc="ITree Initialize">
    public JTree getTree() {
        return this.projectNode.getTree();
    }

    public JPopupMenu getPopupMenu() {
        return this.popupMenu;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="IContextCommand Initialize">
    public void DoCommand(ContextCommandItem item) {
        if (item.getText().contains("Delete Document")) {
            this.projectNode.remove(this);
        }

        if (item.getText().contains("Rename Document")) {
            AddDialog add = new AddDialog(null, true, "Применить", "Переименовать документ");
            add.setValue(this.document.getName());
            add.setVisible(true);
            if (AddDialog.DISPOSE_ON_CLOSE != add.getDefaultCloseOperation()) {
                this.document.setName(add.getValue());
                this.setUserObject(this.document.getName());
            }
        }
        this.getTree().updateUI();
    }
    // </editor-fold>

    public IDocument getDocument() {
        return this.document;
    }

    
}
